*Recoding ZA3066 

use "Data\ZA3066\ZA3066_v4-0-0.dta" , clear

*Support for democracy
gen $idea_democracy				= v86	

*define missing values
mvdecode $idea_democracy, mv(9=.a\8=.b)  //.a = no answer; .b= = don't know)

*reverse scale
replace $idea_democracy = (6-idea_democracy)/5 if !missing($idea_democracy)	 

*Support for liberal democracy
gen $libdem_opposition			= v170k
gen $libdem_freeopinion			= v170h
gen $libdem_partyopportunity	= v170o
* libdem_criticism libdem_conflict libdem_generalwill not asked

*Trust Institutions
gen $trust_parties = v220h
gen $trust_bundestag = v220a
gen $trust_government = v220c
gen $trust_verfassungsgericht = v220b



*define missing values
mvdecode $libdem_opposition $libdem_freeopinion $libdem_partyopportunity ///
	$trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht, ///
	mv(9=.a\8=.b)  //.a = no answer; .b= = don't know)


*Standardize range from 0 to 1; higher values indicate higher support for democracy
*reverse scale if necessary and standardize

foreach var of varlist $libdem_opposition $libdem_freeopinion $libdem_partyopportunity {
	replace `var'	= (`var'-1)/4 if !missing(`var')
}

foreach var of varlist $trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht {
	replace `var' = (`var' - 1)/4 if !missing(`var')
}
	
*Sociodemographic Controls
gen			$gender 			= vsex-1

gen		 	$age				= 1998-vjahr							  	//survey year - year of birth	

gen 		$education			= vbildga
*define missing values
mvdecode	$education, mv(9=.a)  //.a = no answer
replace 	$education 			= 0 if $education 	<= 4 & !missing($education) // Still in School, none, Hautpschule, Mittlere Reife, = 0
replace 	$education 			= 1 if $education 	>=5  & !missing($education) // Abitur/EOS; university = 1

gen 		$eastwest 				= vsplitwo-1

*Weight:  Repraesentativgewicht Gesamtdeutschland Vorwahl oder Nachwahl (mit Ost-West-Gewi

gen weight = vgvwnwow

*Party ID
*Did R lean towards  a left, right or other party?
*1: right, (CDU/CSU, FDP, AfD), 0: left (Left/PDS, Greens, SPD), -1: other
gen $partyid_category		= 1 	if inlist(vpid, 1, 2, 4)
replace $partyid_category	= 0 	if inlist(vpid, 3, 5, 7)
replace $partyid_category	= -1 	if inlist(vpid, 6, 9)


*Party Vote
*Did R lean towards  a left, right or other party?
*1: right, (CDU/CSU, FDP, AfD), 0: left (Left/PDS, Greens, SPD), -1: other
gen $partyvote_category		= 1 	if inlist(v64, 1, 3)
replace $partyvote_category	= 0 	if inlist(v64, 2, 4, 6)
replace $partyvote_category	= -1 	if inlist(v64, 5, 7, 8, 9, 10, 11, 12)

*Meta variables
gen 		$dataset 					= "ZA3066"
gen 		$year 						= 1998

*check
tab $idea_democracy v86, m
tab $libdem_opposition v170k, m
tab $libdem_freeopinion v170h, m
tab $libdem_partyopportunity v170o, m
tab $gender vsex, m
tab $age vjahr, m
tab $education vbildga, m
tab $eastwest vsplitwo, m

*Save recode dataset
keep $idea_democracy $libdem_opposition	$libdem_freeopinion 					///
	$trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht ///
	$libdem_partyopportunity $gender $age $education $eastwest $dataset $year  weight	$partyid_category	$partyvote_category	
save ZA3066_recoded.dta, replace


